package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.Toast;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.CoinTypeList;
import com.chain.BrotherExchange.data.model.CountryCodeList;
import com.chain.BrotherExchange.ui.adapter.SelectorListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.ui.listener.OnSpecificEditorActionListener;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.viewmodel.SelectedItemViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;

/**
 * 选择列表：
 * 国家/地区
 * 币种
 */
public class SelectedListActivity extends AbsNetworkStateActivity {

    @Bind(R.id.text_list) RecyclerView mTextList;
    @Bind(R.id.search_edit_text) EditText mSearchEditText;

    private int mPageType;
    private SelectedItemViewModel mViewModel;

    public static void launch(Activity activity, int type) {
        activity.startActivityForResult(new Intent(activity, SelectedListActivity.class)
                        .putExtra(C.EXTRA_PAGE_TYPE, type),
                C.REQ_REGION_COIN_TYPE
        );
    }

    @Override
    public int getTitleLayoutRes() {
        return R.layout.layout_default_title_bar;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_selected_list;
    }

    @Override
    protected void initActions() {
        // 初始化页面类型
        mPageType = getIntent().getIntExtra(C.EXTRA_PAGE_TYPE, 0);

        // 初始化标题
        ActivityBarHelper.init(this, mPageType == C.VALUE_COIN_TYPE
                ? R.string.title_select_coin
                : R.string.title_countries_regions);

        // 视图模型
        mViewModel = ViewModelProviders.of(this).get(SelectedItemViewModel.class);

        // 页面列表数据
        initListData();

        // 键盘提交搜索结果
        mSearchEditText.setOnEditorActionListener(new OnSpecificEditorActionListener(
                EditorInfo.IME_ACTION_SEARCH, (v, actionId, event) -> search()));

        // 加载动画
//        mTextList.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(this,
//                R.anim.layout_animation_slide_right));
    }

    private void initListData() {
        switch (mPageType) {
            case C.VALUE_COIN_TYPE:
                initCoinType();
                break;
            case C.VALUE_REGION_TYPE:
                initRegion();
                break;
        }
    }

    private void initRegion() {
        mViewModel.countryCode().observe(this, resource ->
                Resource.load(this, resource, this, result -> {
                    SelectorListAdapter adapter = new SelectorListAdapter(
                            mViewModel.convertCountryToStrings(result),
                            (parent, view, position, id) -> {
                                CountryCodeList.DataBean.ListBean bean = result.get(position);
                                setResult(RESULT_OK, new Intent().putExtra(C.EXTRA_REGION, bean));
                                finish();
                            });

                    mTextList.setAdapter(adapter);
                })
        );
    }

    private void initCoinType() {
        mViewModel.allCoinType().observe(this, resource ->
                Resource.load(this, resource, this, result -> {
                    SelectorListAdapter adapter = new SelectorListAdapter(
                            mViewModel.convertCoinTypeToStrings(result),
                            (parent, view, position, id) -> {
                                CoinTypeList.DataBean.ListBean bean = result.get(position);
                                setResult(RESULT_OK, new Intent().putExtra(C.EXTRA_COIN_TYPE, bean));
                                finish();
                            });

                    mTextList.setAdapter(adapter);
                })
        );
    }

    private boolean search() {
        Toast.makeText(this, "search", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onAnewRequest() {
        initListData();
    }
}
